/*
 * Computer.cc
 *
 *  Created on: Apr 3, 2012
 *      Author: victor
 */

#include "Computer.h"

Computer::Computer(IOCard* io1, IOCard* io2, IOCard* io3) : Thread("Consumer"){
	panoramaImage = new byte[9];
	ioCard1 = io1;
	ioCard2 = io2;
	ioCard3 = io3;
	hasImage = false;
	imagesReceived = 0;
}

Computer::~Computer(){
	delete ioCard1;
	delete ioCard2;
	delete ioCard3;
	delete[] panoramaImage;
}

bool Computer::hasPanoramaImage(){
	return hasImage;
}

void Computer::readFromIOCard(IOCard* card){
	if (CameraBuffer* tmpBuffer = dynamic_cast<CameraBuffer*> (card->getReadBuffer())){
		if (!panoramaImage[(int)tmpBuffer->getCamera()]){
			imagesReceived++;
		}
			panoramaImage[(int)tmpBuffer->getCamera()] = tmpBuffer->getBeeld();
	}

	if (imagesReceived == 9){
		hasImage = true;
	}
}

void Computer::run(){
	for (;;){
		readFromIOCard(ioCard1);
		readFromIOCard(ioCard2);
		readFromIOCard(ioCard3);
	}
}


byte* Computer::getPanoramaImage(){
	byte* tmpImage = new byte[9];
	for (int i = 0; i < 9; i++){
		tmpImage[i] = panoramaImage[i];
		panoramaImage[i] = 0;
	}
	hasImage = false;
	imagesReceived = 0;
	return tmpImage;
}


